var app = new Vue({
	el: "#app",
	methods: {
		update_file: function() {

		},
		upload_file: function() {
			var img_file = document.getElementById("book-img");
			if (!img_file.files) {
				alert("请选择文件");
				return;
			}
			if (img_file.files.length == 0) {
				alert("请选择文件");
				return;
			}
			var form_data = new FormData();
			form_data.append(img_file.files[0].name, img_file.files[0]);
			var xhr = new XMLHttpRequest();

			var ajax_config = static_ajax.post("/uploadfile/uploadimg").result();

			var that = this;
			xhr.onload = function(e) {
				var result = JSON.parse(e.target.responseText);
				that.img_file_id = result.media_id;
				that.create();
			};

			xhr.open("POST", ajax_config.url, true);
			xhr.send(form_data);


		},
		create: function() {
			static_ajax.post("/wordbook/new")
				.data({
					img: this.img_file_id,
					name: this.book_name,
					message: this.book_message
				})
				.success(function(e) {
					alert("创建成功");
					window.location.href = "index.html";
				})
				.ajax();
		}
	},
	data: {
		img_file_id: "",

		book_name: "",
		book_message: ""
	}
})

function uploadProgress(e) {
	//alert(e.loaded);
}